﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using CLFramework.Configuration;

namespace CLFramework.Infrastructure.PlugIns
{
    public class PlugInConfiguration : ConfigurationBase
    {
        List<PluginContainer> plugIns = new List<PluginContainer>();
        PluginEngine pluginEngine = null;

        public PlugInConfiguration()
            : base()
        { }

        public PlugInConfiguration(XmlNode node)
            : base(node)
        {
            foreach (XmlNode n in node.ChildNodes)
            {
                if (n.Name.Equals("plugIn", StringComparison.CurrentCultureIgnoreCase))
                {
                    XmlAttribute attribute = n.Attributes["type"];
                    Type type = Type.GetType(attribute.Value);
                    if (type != null)
                    {
                        PluginContainer plugIn = new PluginContainer(type, n);
                        plugIns.Add(plugIn);
                    }
                }
            }
        }

        public static PlugInConfiguration GetConfig(XmlNode node)
        {
            return GetConfig<PlugInConfiguration>(node, false) as PlugInConfiguration;
        }


        public List<PluginContainer> PlugIns
        {
            get { return plugIns; }
        }

        public PluginEngine PluginEngine
        {
            get {
                if (pluginEngine == null)
                {
                    pluginEngine = new PluginEngine(PlugIns);
                }
                return pluginEngine;
            }
        }
    }
}
